import 'dart:async';

import 'package:just_audio/just_audio.dart';

class AudioManager {
  static final AudioManager _instance = AudioManager._internal();
  factory AudioManager() => _instance;
  AudioManager._internal();

  final AudioPlayer _backgroundPlayer = AudioPlayer();
  final AudioPlayer _effectPlayer = AudioPlayer();
  Future<void> playBackgroundMusic(String path) async {
    await _backgroundPlayer.setLoopMode(LoopMode.all);
    await _backgroundPlayer.setAsset(path);
    await _backgroundPlayer.setVolume(0.5);
    await _backgroundPlayer.play();
  }

  Future<void> playSoundEffect(String soundName) async {
    try {
      await _effectPlayer.stop();
      await _effectPlayer.setLoopMode(LoopMode.off);
      await _effectPlayer.setAsset(soundName);
      await _effectPlayer.setVolume(1);
      await _effectPlayer.play();
    } catch (e) {
      print('播放音效时出错: $e');
    }
  }

  Future<void> stopBackgroundMusic() async {
    await _backgroundPlayer.stop();
  }
  
}
